package com.asdc.autoscanfiles.service;

import com.asdc.autoscanfiles.constant.DictConstants;
import com.asdc.autoscanfiles.entity.Dict;
import com.asdc.autoscanfiles.repository.DictRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhangbingyue
 * @Description //TODO $
 * @Date $ $
 * @Param $
 * @return $
 **/
@Service
@Scope("singleton")
public class DictService {

	@Autowired
	DictRepository dictRepository;
	private Map<String, String> provinceMap = null;

	public Map<String, String> getProvinceMap() {
		if (this.provinceMap == null) {
			
			List<Dict> dicts = this.dictRepository.queryParentByType(DictConstants.TYPE_PROVINCE);
			if(!CollectionUtils.isEmpty(dicts)) {
				this.provinceMap = new HashMap<>();
				for(Dict dict : dicts) {
					this.provinceMap.put(dict.getCode(), dict.getText());
				}
			}
		}
		return this.provinceMap;
	}

	public List<Dict> queryByCodeAndType(String code, String type) {
		return dictRepository.queryByCodeAndType(code, type);
	}

	public List<Dict> queryParentByType(String type) {
		return dictRepository.queryParentByType(type);
	}

	public List<Dict> queryByType(String type, Integer parentId) {
		return dictRepository.queryByType(type, parentId);
	}

	/**
	 * 查询区站号对应的省市，以map的形式返回
	 *
	 * @param Ptype
	 *            0
	 * @param Ctype
	 *            1
	 * @return
	 */
	public Map<String, String> getList(String Ptype, String Ctype) {
		Map map = new HashMap();
		Map m = new HashMap();
		List<Dict> dicts = dictRepository.queryParentByType(Ptype);
		for (Dict dict : dicts) {
			m.put(dict.getId(), dict.getText());
		}

		List<Dict> dicts1 = dictRepository.queryByType(Ctype);
		for (Dict dict : dicts1) {
			if (m.containsKey(dict.getParentId())) {
				map.put(dict.getCode(), m.get(dict.getParentId()));
			}
		}

		return map;
	}

}
